<?php
/**
 * [LupMisNotAllowedSync]
 * 本代码为系统自动生成代码，请根据自己业务进行修改;
 * 生成时间 2022-02-18 18:16:27;
 * 版权所有 2020-2022 lizhongwen，并保留所有权利;
 * 说明: 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；不允许对程序代码以任何形式任何目的的再发布;
 * 作者: 中文Lee;
 * 作者主页: http://www.lizhongwen.com;
 * 邮箱: 360811363@qq.com;
 * QQ: 360811363;
 */
namespace modules\sys\service\impl;

use application\core\Result;
use application\core\SnowFlake;
use modules\sys\mapper\OrgMapper;
use modules\sys\service\OrgService;

class OrgServiceImpl implements OrgService
{

    public function lists(array $data): array
    {

        $orgRs = OrgMapper::lists($data);
        foreach ($orgRs as $k => $v) {
            $orgRs[$k]['pId'] = $v['pid'];
        }
        return $orgRs;
    }

    public function add(array $data): array
    {
        $data['id'] = SnowFlake::instance()->nextId();
        $position = $data['position'];
        $pid = $data['pid'];
        $sort = 1000000000;
        if ($position == -1) {
            //末尾创建
            $queryByMaxSort = OrgMapper::queryByMaxSort(['pid' => $pid]);
            if (!empty($queryByMaxSort)) {
                $sort = $queryByMaxSort['sort'];
            }
            $sort = $sort + 1;
        } else {
            //开头创建
            $queryByMinSort = OrgMapper::queryByMinSort(['pid' => $pid]);
            if (!empty($queryByMaxSort)) {
                $sort = $queryByMinSort['sort'];
            }
            $sort = $sort - 1;
        }
        $data['sort'] = $sort;
        $data['status'] = 1;
        $data['leader'] = '';
        $data['phone'] = '';
        $data['email'] = '';
        $data['address'] = '';
        $data['remarks'] = '';

        $add = OrgMapper::add($data);
        if ($add == 0) {
            return Result::fail(false,[], "添加失败");
        }
        return Result::success(true,['id'=>$data['id'],'pid'=>$pid,'name'=>$data['name']], "添加成功");
    }

    public function del(array $data): array
    {


        if($data['ids']==1){
            return Result::fail(false,[], "跟机构不能删除！");
        }
        //先查询是否有下级菜单
        $listsByPidRs = OrgMapper::listsByPid(['pid' => $data['ids']]);
        if (!empty($listsByPidRs)) {
            return Result::fail(false,[], "请先删除下级机构！");
        }

        $edit = OrgMapper::del(['id' => $data['id']]);
        if ($edit == 0) {
            return Result::fail(false,[], "数据不存在");
        }
        return Result::success(true,[], "删除成功");
    }

    public function edit(array $data): array
    {
        $edit = OrgMapper::edit($data);
        if ($edit == 0) {
            return Result::fail(false,[], "未修改任何数据");
        }
        return Result::success(true,[], "修改成功");
    }
    public function move(array $data): array
    {
        $id = $data['id'];
        $targetId = $data['target_id'];
        $currentRs = OrgMapper::query(['id' => $id]);
        $targetRs = OrgMapper::query(['id' => $targetId]);
        OrgMapper::move(['id'=>$id,'sort'=>$targetRs['sort']]);
        OrgMapper::move(['id'=>$targetId,'sort'=>$currentRs['sort']]);
        return Result::success(true,[], "操作成功");
    }
    public function editName(array $data): array
    {
        $edit = OrgMapper::editName($data);
        if ($edit == 0) {
            return Result::fail(false,[], "未修改任何数据");
        }
        return Result::success(true,[], "修改成功");
    }

    public function query(array $data): array
    {
        return Result::success(false,OrgMapper::query($data), "修改成功");
    }
}

